package com.fzhucc.modules.lesson.rest;

import com.fzhucc.annotation.Log;
import com.fzhucc.modules.lesson.domain.LessonSecondChapter;
import com.fzhucc.modules.lesson.domain.dto.LessonSecondChapterQueryCriteria;
import com.fzhucc.modules.lesson.service.LessonSecondChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author yyg
* @date 2022-12-08
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程:文件二级分类管理")
@RequestMapping("/api/lessonSecondChapter")
public class LessonSecondChapterController {

    private final LessonSecondChapterService lessonSecondChapterService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('lessonSecondChapter:list')")
    public void exportLessonSecondChapter(HttpServletResponse response, LessonSecondChapterQueryCriteria criteria) throws IOException {
        lessonSecondChapterService.download(lessonSecondChapterService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询lessonSecondChapter")
    @ApiOperation("查询lessonSecondChapter")
    @PreAuthorize("@el.check('lessonSecondChapter:list')")
    public ResponseEntity<Object> queryLessonSecondChapter(LessonSecondChapterQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(lessonSecondChapterService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("课程赋予文件二级分类")
    @ApiOperation("课程赋予文件二级分类")
    @PreAuthorize("@el.check('lessonSecondChapter:add')")
    public ResponseEntity<Object> createLessonSecondChapter(@Validated @RequestBody LessonSecondChapter resources){
        return new ResponseEntity<>(lessonSecondChapterService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改lessonSecondChapter")
    @ApiOperation("修改lessonSecondChapter")
    @PreAuthorize("@el.check('lessonSecondChapter:edit')")
    public ResponseEntity<Object> updateLessonSecondChapter(@Validated @RequestBody LessonSecondChapter resources){
        lessonSecondChapterService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除lessonSecondChapter")
    @ApiOperation("删除lessonSecondChapter")
    @PreAuthorize("@el.check('lessonSecondChapter:del')")
    public ResponseEntity<Object> deleteLessonSecondChapter(@RequestBody Integer[] ids) {
        lessonSecondChapterService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}